package data_creation;

import java.util.ArrayList;
import java.util.Iterator;

import poki.model.Card;
import poki.model.CardSet;

public class SetOfCardSets {


	private ArrayList<CardSet> cardSetList = new ArrayList<CardSet>();
	
	public SetOfCardSets() {
		
	}
	
	public SetOfCardSets(ArrayList<CardSet> cardSetList){
		this.cardSetList = cardSetList;
	}
	
	public ArrayList<CardSet> getCardSetList() {
		return this.cardSetList;
	}
	
	public void addCardSet(CardSet cardSet)
	{
		this.getCardSetList().add(cardSet);
	}
	
	public SetOfCardSets merge(SetOfCardSets setOfCardSets) {

		SetOfCardSets newSetOfCardSets = new SetOfCardSets();

		for (CardSet cardSet : this.getCardSetList()) {
			newSetOfCardSets.addCardSet(cardSet);
		}

		for (CardSet cardSet : setOfCardSets.getCardSetList()) {
			newSetOfCardSets.addCardSet(cardSet);
		}

		return newSetOfCardSets;
	}
	
	//Entfernt aus dem aktuellen Kartenset die Schnittmenge beider Kartensets und gibt ein neues Kartenset aus
	public SetOfCardSets disMerge(SetOfCardSets setOfCardSets) {
		
		SetOfCardSets newSetOfCardSets = new SetOfCardSets();
		
		for(CardSet cardSet : this.getCardSetList()){
			newSetOfCardSets.addCardSet(cardSet);
		}
		for (CardSet cardSet : setOfCardSets.getCardSetList()){
			newSetOfCardSets.removeCardSet(cardSet);
			}
		
		return newSetOfCardSets;
	}

	
	
	
	@Override
	public String toString() {
		
		String output = "";

		if(this.getCardSetList().size() > 0) {
			output += this.getCardSetList().get(0).toString();
			for(int i = 1; i < this.getCardSetList().size(); i++) {
				output += "// ";
				output += this.getCardSetList().get(i).toString();
			}
		}

		return output;
	}

	//Überprüft ob eine spezielle Karte (gleiche Farbe gleiche Zahl) enthalten ist
	public boolean containsCardSet(CardSet cardSet){
		
		for(CardSet cardSet2 : this.getCardSetList()){
			if(cardSet2.equals(cardSet)){
				return true;
			}
		}
		
		return false;
	}
	
	// Entfernt eine spezielle Karte (gleiche Farbe gleiche Zahl), muss nicht
	// die Selbe Karteninstanz (gleiches Objekt) sein
	public void removeCardSet(CardSet cardSet) {

		Iterator<CardSet> cardSetListIterator = this.getCardSetList().listIterator();

		while (cardSetListIterator.hasNext()) {
			CardSet cardSet2 = cardSetListIterator.next();
			if (cardSet2.equals(cardSet)) {
				cardSetListIterator.remove();
			}
		}
	}
	
	public boolean setOfCardSetsIsEmpty(){
		
		boolean isEmpty = false;
		
		if(this.getCardSetList().size() == 0){
			isEmpty = true;
		} else {
			for(CardSet cardSet : this.getCardSetList()){
				if(cardSet.getCardList().size() == 0){
					isEmpty = true;
				}
			}
		}
		
		return isEmpty;
	}

}
